package com.anda.driver.util;

import java.io.IOException;
import java.util.LinkedHashMap;

import android.content.Context;

import com.anda.driver.locator.R;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonGoogleRegionUtil {
	private Context context;
	private static GoogleRegion googleRegion;
	private static JsonGoogleRegionUtil jsonGoogleRegionUtil;
	
	private JsonGoogleRegionUtil(Context context){
		this.context = context;
		getObjectFromJson();
	}
	public static JsonGoogleRegionUtil getInstance(Context context){
		if ( jsonGoogleRegionUtil == null ) {
			jsonGoogleRegionUtil = new JsonGoogleRegionUtil(context);
		}
		return jsonGoogleRegionUtil;
	}
	
	private void getObjectFromJson(){
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String json = CommUtil.readJsonData(context, R.raw.googleregion);
			googleRegion = objectMapper.readValue(json, GoogleRegion.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	static class GoogleRegion{
		private String status;
		private LinkedHashMap<String, LinkedHashMap<String, String>> data;
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		public LinkedHashMap<String, LinkedHashMap<String, String>> getData() {
			return data;
		}
		public void setP(LinkedHashMap<String, LinkedHashMap<String, String>> data) {
			this.data = data;
		}
	}
	
	public GoogleRegionProvince getObjectFromName(String pname, String cname){
		if (googleRegion != null && googleRegion.getStatus().equals("OK")){
			for(String pid: googleRegion.getData().keySet()){
				if ( googleRegion.getData().get(pid).containsKey("n") &&
						googleRegion.getData().get(pid).get("n").equals(pname)) {
					for(String cid: googleRegion.getData().get(pid).keySet()) {
						if ( googleRegion.getData().get(pid).get(cid).equals(cname)) {
							return new GoogleRegionProvince(
								pid, pname, cid, cname
							);
						}
					}
				}
			}
		}
		return null;
	}
	
	public class GoogleRegionProvince{
		private String pid;
		private String pname;
		private String cid;
		private String cname;
		
		public GoogleRegionProvince(){
		}
		
		public GoogleRegionProvince(String pid, String pname, String cid,
				String cname) {
			super();
			this.pid = pid;
			this.pname = pname;
			this.cid = cid;
			this.cname = cname;
		}

		public String getPid() {
			return pid;
		}
		public void setPid(String pid) {
			this.pid = pid;
		}
		public String getPname() {
			return pname;
		}
		public void setPname(String pname) {
			this.pname = pname;
		}
		public String getCid() {
			return cid;
		}
		public void setCid(String cid) {
			this.cid = cid;
		}
		public String getCname() {
			return cname;
		}
		public void setCname(String cname) {
			this.cname = cname;
		}

		@Override
		public String toString() {
			return "GoogleRegionProvince [pid=" + pid + ", pname=" + pname
					+ ", cid=" + cid + ", cname=" + cname + "]";
		}
		
	}
}
